<template>
	<view class="authentication-user">
		<view class="page-tip">请完善以下资料并通过实名认证</view>
		<xzfx-panel :showTitle="false">
			<view slot="content">
				<view class="upload-panel">
					<view class="upload-panel-title">身份证人像面<text>(必传)</text></view>
					<view class="upload-panel-box">
						<view v-if="!form.idImgFront" class="upload-panel-tip" @click.stop="uploadFn('idImgFront')">
							<view class="upload-panel-icon xzfx_icon_paizhao"></view>
							<view class="upload-panel-text">点击上传 身份证人像面图片</view>
						</view>
						<image v-else class="upload-panel-image" :src="form.idImgFront"></image>
					</view>
				</view>
				<view class="upload-panel">
					<view class="upload-panel-title">身份证国徽面<text>(必传)</text></view>
					<view class="upload-panel-box">
						<view v-if="!form.idImgBack" class="upload-panel-tip" @click.stop="uploadFn('idImgBack')">
							<view class="upload-panel-icon xzfx_icon_paizhao"></view>
							<view class="upload-panel-text">点击上传 身份证国徽面图片</view>
						</view>
						<image v-else class="upload-panel-image" :src="form.idImgBack"></image>
					</view>
				</view>
			</view>
		</xzfx-panel>
		<view class="submit-button" @click.stop="submitFn">立即认证</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: uni.getStorageSync("userInfo"),
				e: null,
				form: {
					idImgFront: "",
					idImgBack: ""
				}
			};
		},
		onLoad(e) {
			this.e = e;
		},
		methods: {
			uploadFn(key){
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					extension: ['.jpg', '.png', '.jpeg'],
					success: (res) => {
						this.$utils.system.loading("上传中，请稍后");
						uni.uploadFile({
							url: 'https://api.sxbchr.com/bchr/app/config/upload',
							filePath: res.tempFilePaths[0],
							name: 'file',
							header: {
								"X-Access-Token": uni.getStorageSync("token") || ""
							},
							success: (response) => {
								if(response.statusCode === 200){
									let data = JSON.parse(response.data)
									if(data.code === 200){
										this.form[key] = data.message
									}else{
										this.$utils.system.tip("上传失败");
									}
								}else{
									this.$utils.system.tip("上传失败");
								}
							},
							fail: (error) => {
								this.$utils.system.tip("上传失败");
							},
							complete: () => {
								uni.hideLoading()
							}
						});
					}
				});
			},
			submitFn(){
				if(!this.form.idImgFront){
					this.$utils.system.tip("请上传身份证人像面图片");
					return
				}
				if(!this.form.idImgBack){
					this.$utils.system.tip("请上传身份证国徽面图片");
					return
				}
				this.$utils.system.loading("正在认证，请稍后");
				uni.login({
					success: response => {
						this.$apis.authentication.user(Object.assign(this.form, {
							code: response.code
						})).then(res => {
							if(this.e.type === "carOwner"){
								this.$go("/pages/authentication/car-owner?delta=2");
							}else{
								this.$utils.system.tip('认证成功');
								this.$apis.user.getInfo().then(res => {
									uni.setStorageSync("userInfo", res);
								}).catch(err => {
									console.log(err)
								})
								setTimeout(() => {
									uni.navigateBack({ delta: 1 });
								}, 3000);
							}
						}).catch(err => {
							console.log(err);
						}).finally(() => {
							setTimeout(() => {
								uni.hideLoading();
							}, 3000)
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #fff !important;
	}
	.authentication-user{
		padding: 10rpx 0 50rpx 0;
		.page-tip{
			margin: $xzfx-spacing;
			font-size: 30rpx;
			color: #333;
		}
		.upload-panel{
			margin-top: 20rpx;
			.upload-panel-title{
				color: #666666;
				font-size: 30rpx;
				padding-bottom: 15rpx;
				text{
					font-size: 28rpx;
					color: $xzfx-theme;
					padding-left: 5rpx;
				}
			}
			.upload-panel-box{
				.upload-panel-tip{
					display: flex;
					justify-content: center;
					align-items: center;
					flex-direction: column;
					border: 2rpx dashed #f5f5f5;
					border-radius: 20rpx;
					height: 300rpx;
					.upload-panel-icon{
						font-size: 100rpx;
						color: $xzfx-theme;
					}
					.upload-panel-text{
						color: #999999;
						font-size: 28rpx;
						margin-top: 10rpx;
					}
				}
				.upload-panel-image{
					width: auto;
					height: 300rpx;
					display: block;
				}
			}
			.upload-panel-box:nth-child(3),
			.upload-panel-box:nth-child(4){
				margin-top: 20rpx;
			}
		}
		.upload-panel:first-child{
			margin-top: 0;
		}
		.submit-button{
			margin: 60rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 90rpx;
			border-radius: 46rpx;
			background: $xzfx-theme;
			color: #FFFFFF;
			font-size: 32rpx;
			padding: 0 40rpx;
			overflow: hidden;
		}
	}
</style>